<?php
namespace app\admin\controller;
use app\common\model;
use app\common\model\GoodsClasses as GoodsClassesModel;

class Goodsclasses extends Base
{
    public function __construct () {
        parent::__construct();
    }

    /*
     列表
     */
    public function classeslist()
    {
        $where = [
            'mall_id' => $this->manager['mall_id']
        ];
        $input = input();
        $page = max(input('page'),1);
        $size = 20;
        if($input['id']) {
            $where['id'] = $input['id'];
        }
        if ($input['keywords']) {
            $where['name'] = array('like', '%' . $input['keywords'] . '%');
        }
        $goodsList = GoodsClassesModel::getPage($where,$page,$size,'ord desc,id desc');
        $total = GoodsClassesModel::getCount($where);
        if(!$goodsList) {
            return returnJson(1,'');
        }
        foreach($goodsList as $k=>$v) {
            if($v['picture_list']) {
                $pics = json_decode($v['picture_list'],true);
                foreach($pics as $kk=>$vv) {
                    $pics[$kk]['url'] = tomedia($vv['path']);
                }
                $goodsList[$k]['picture_list'] = $pics;
            }
            $goodsList[$k]['picture'] = tomedia($v['picture']);
            $goodsList[$k]['content'] = !empty($v['content'])?$v['content']:'';
        }
        return returnJson(0,'',[
            'list' => $goodsList,
            'size' => $size,
            'total' => $total
        ]);
    }


    /* 新增商品/修改商品 */
    public function edit () {
        if(request()->isPost()) {
            $input = input();
            if(!isset($input['name']) || !isset($input['price']) || !isset($input['picture_list'])) {
                return returnJson(1,'请填写完整');
            }
            $pics = json_decode($input['picture_list'],true);
            $time = time();
            $data = array(
                'mall_id' => $this->manager['mall_id'],
                'name' => $input['name'],
                'price' => $input['price'],
                'picture_list' => $input['picture_list'],
                'picture' => $pics[0]['path'],
                'ord' => $input['ord'],
                'desc' => $input['desc'],
                'content' => $input['content'],
                'onsale' => $input['onsale'],
                'sale_val' => $input['sale_val'],
                'stock' => $input['stock'],
                'update_time'=>$time
            );
            if($input['id']) {
                $res = GoodsClassesModel::update($data,['id'=>['eq',$input['id']]]);
            }else{
                $data['create_time']=$time;
                $res = GoodsClassesModel::setInsertGetId($data);
            }
            if($res) {
                return returnJson(0,'操作成功');
            }
            return returnJson(1,'网络异常，请重试');
        }
    }

    /* 删除商品 */
    public function delete () {
        if($this->manager['mall_id']!=0) {
            //return returnJson(1,'您无权修改');
        }
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $goods = $this->goodsModel->getOne(['id'=>$input['id']]);
        if(!$goods) {
            return returnJson(1,'商品不存在');
        }
        if($goods['mall_id']!=$this->manager['mall_id']) {
            return returnJson(0,'无权修改');
        }
        $res = $this->goodsModel->where(['id'=>$input['id']])->delete();
        if($res) {
            $pics = json_decode($goods['picture_list'],true);
            foreach($pics as $v) {
                //model('qiniu')->remove($v['path']); 商品图片不能删除
            }
            return returnJson(0,'已删除');
        }else{
            return returnJson(1,'删除失败，请重试');
        }
    }
}
